import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/http.dart';
import 'package:osg/controllers/my_method.dart';

class LoginType extends StatefulWidget {
  @override
  _DefaultTextFieldState createState() => _DefaultTextFieldState();
}

class _DefaultTextFieldState extends State<LoginType> {
// class LoginType extends StatelessWidget {
  final RxBool _isLogin = true.obs;
  bool _obscureText = true;

  final TextEditingController phone =
      TextEditingController(text: '15918727922');
  final TextEditingController password =
      TextEditingController(text: '12345678');

  @override
  void dispose() {
    phone.dispose();
    password.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          Image.asset(
            'static/images/dengli.png',
            fit: BoxFit.fitWidth,
          ),
          Container(
            height: 30,
            transform: Matrix4.translationValues(0.0, -15.0, 0.0),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(30),
                color: Color(0XFFFFFFFF)),
          ),
          Container(
            transform: Matrix4.translationValues(0.0, -15.0, 0.0),
            child: Image.asset(
              'static/images/open.png',
              fit: BoxFit.fitWidth,
              width: 140,
            ),
          ),
          Obx(
            () => _isLogin.value
                ? Container(
                    margin: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                    child: Row(
                      children: [
                        SizedBox(
                          width: 15,
                        ),
                        Image.asset(
                          'static/images/guoqi.png',
                          fit: BoxFit.fitWidth,
                          width: 27,
                        ),
                        SizedBox(
                          width: 5,
                        ),
                        Text('+86', style: TextStyle(fontSize: 14)),
                        Expanded(
                            child: TextField(
                          controller: phone,
                          style: TextStyle(fontSize: 14),
                          decoration: InputDecoration(
                            isCollapsed: true, //设置高度
                            contentPadding: EdgeInsets.all(15),
                            hintStyle: TextStyle(color: Color(0xff999999)),
                            border: InputBorder.none, // 隐藏输入框默认边框
                            hintText: '请输入手机号', // 提示文本
                          ),
                        ))
                      ],
                    ),
                  )
                : Container(
                    margin: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                    child: TextField(
                      style: TextStyle(fontSize: 14),
                      decoration: InputDecoration(
                        isCollapsed: true, //设置高度
                        contentPadding: EdgeInsets.all(15),
                        hintStyle: TextStyle(color: Color(0xff999999)),
                        border: InputBorder.none, // 隐藏输入框默认边框
                        hintText: '请输入电子邮箱地址', // 提示文本
                      ),
                    )),
          ),
          Container(
              margin: EdgeInsets.symmetric(horizontal: 30),
              decoration: BoxDecoration(
                  border: Border(bottom: BorderSide(color: Color(0XFFDDDDDD)))),
              child: TextField(
                obscureText: true,
                controller: password,
                style: TextStyle(fontSize: 14),
                decoration: InputDecoration(
                  // suffixIcon: IconButton(
                  //   // 使用IconButton作为后缀图标按钮
                  //   icon: Icon(
                  //     size: 15,
                  //     _obscureText
                  //         ? Icons.visibility_off
                  //         : Icons.visibility, // 根据_obscureText的值切换图标
                  //   ),
                  //   onPressed: () {}, // 点击图标时调用_toggleVisibility方法
                  // ),
                  isCollapsed: true, //设置高度
                  contentPadding: EdgeInsets.all(15),
                  hintStyle: TextStyle(color: Color(0xff999999)),
                  border: InputBorder.none, // 隐藏输入框默认边框
                  hintText: '请输入密码', // 提示文本
                ),
              )),
          Container(
              margin: EdgeInsets.only(right: 30, top: 15, bottom: 30),
              alignment: Alignment.centerRight,
              child: GestureDetector(
                onTap: () => {_isLogin.value = !_isLogin.value},
                child: Obx(() => _isLogin.value
                    ? Text('邮箱登录',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF0A72D9)))
                    : Text('手机登录',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF0A72D9)))),
              )),
          GestureDetector(
            onTap: () {
              if (!phone.text.isNotEmpty) {
                Get.find<HTTPS>().showTost('输入手机号');
                return;
              }
              if (!password.text.isNotEmpty) {
                Get.find<HTTPS>().showTost('输入密码');
                return;
              }

              Get.find<HTTPS>().postData('/login',
                  {'phone': phone.text, 'password': password.text}).then((res) {
                if (res != null) {
                  Get.back();
                  Get.find<MyMethod>().myset('token', res['token']);
                  Get.toNamed('/');
                }
              });
            },
            child: Container(
                width: 300,
                height: 50,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular((50)),
                  gradient: LinearGradient(
                    colors: [
                      Color(0xff2189E7),
                      Color(0xffBF67F8),
                    ], // 定义颜色列表
                    begin: Alignment.topLeft, // 渐变开始的位置
                    end: Alignment.bottomRight, // 渐变结束的位置
                  ),
                ),
                child: Text('登 录',
                    style: TextStyle(fontSize: 16, color: Color(0XFFFFFFFF)))),
          ),
          SizedBox(
            height: 20,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text('注册新用户',
                  style: TextStyle(fontSize: 12, color: Color(0XFF999999))),
              Text('忘记密码？',
                  style: TextStyle(fontSize: 12, color: Color(0XFF999999))),
            ],
          ),
          Container(
            margin: EdgeInsets.only(left: 100, right: 100, top: 50),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Image.asset(
                  'static/images/weixin.png',
                  width: 40,
                  fit: BoxFit.fitWidth,
                ),
                Image.asset(
                  'static/images/pingguo.png',
                  width: 40,
                  fit: BoxFit.fitWidth,
                ),
              ],
            ),
          ),
          Expanded(child: SizedBox()),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Transform.scale(
                scale: 0.7,
                child: Radio(
                    value: 1,
                    groupValue: 1,
                    onChanged: (v) => {},
                    activeColor: Color(0XFF0A72D9)),
              ),
              Text('已阅读并同意', style: TextStyle(fontSize: 11)),
              Text('《用户服务协议》',
                  style: TextStyle(fontSize: 11, color: Color(0XFF0A72D9))),
              Text('与', style: TextStyle(fontSize: 11)),
              Text('《隐私权政策》',
                  style: TextStyle(fontSize: 11, color: Color(0XFF0A72D9))),
            ],
          )
        ],
      ),
    );
  }
}
